package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.MessageListDTO;
import com.hoshino.momochi.model.domain.MessageList;
import com.hoshino.momochi.service.MessageListService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.SomeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
public class MessageListController {

    private final String USER_ID = CodeEnum.USERID.getMessage();

    @Autowired
    private MessageListService messageListService;

    /**
     * 获取当前用户 的 消息对话列表
     *
     * @param request
     * @return 含有UserDetails 的 messageList
     */
    @RequestMapping("api/messageList/getList")
    public JsonResponse getUserDetailsList(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<MessageList> messageLists = messageListService.getNowUserMessageUserDetails(uid);
        return JsonResponse.success(SomeUtils.sortByCreateTime(messageLists), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 将 用户 添加到 消息列表
     *
     * @param request
     * @param messageListDTO
     * @return
     */
    @RequestMapping("api/messageList/addList")
    public JsonResponse addUserInMessageList(HttpServletRequest request, @RequestBody MessageListDTO messageListDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long tid = messageListDTO.getTarget_id();
        if (Objects.isNull(tid)) {
            return JsonResponse.success(CodeEnum.NOTNULL.getCode(), CodeEnum.NOTNULL.getMessage());
        }
        // 校验 消息列表中是否存在
        Long check = messageListService.checkMessageListExist(uid, tid);
        MessageList addItem = new MessageList();
        addItem.setCreateTime(new Date());
        addItem.setUserId(uid);
        addItem.setTargetId(tid);
        if (Objects.nonNull(check)) {
            // 存在则更新
            messageListService.updateCreateTime(addItem);
        }
        // 不存在则新增
        messageListService.addMessageList(addItem);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 删除 与指定用户的 消息列表 但是（不会删除聊天记录！）
     *
     * @param request
     * @param messageListDTO
     * @return
     */
    @RequestMapping("api/messageList/delTarget")
    public JsonResponse delTargetUser(HttpServletRequest request, @RequestBody MessageListDTO messageListDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long tid = messageListDTO.getTarget_id();
        if (Objects.isNull(tid)) {
            return JsonResponse.success(CodeEnum.NOTNULL.getCode(), CodeEnum.NOTNULL.getMessage());
        }
        messageListService.delExistMessageList(uid, tid);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }
}
